//
//  FishingEventView.m
//  WHTestDome
//
//  Created by Assistant on 9/22/25.
//

#import "FishingEventView.h"

@interface FishingEventView ()

@property (nonatomic, strong) UIView *headerCard;
@property (nonatomic, strong) UIView *detailsCard;
@property (nonatomic, strong) UIView *announcementCard;
@property (nonatomic, strong) UILabel *registrationLabel;
@property (nonatomic, strong) UILabel *countdownLabel;
@property (nonatomic, strong) UITextView *announcementTextView;

@end

@implementation FishingEventView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = [UIColor systemGroupedBackgroundColor];
    
    // 滚动视图
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self addSubview:self.scrollView];
    
    self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width,self.bounds.size.height)];
    [self.scrollView addSubview:self.contentView];
}

- (void)updateWithEvent:(FishingEvent *)event {
    [self setupHeaderCardWithEvent:event];
    [self setupDetailsCardWithEvent:event];
    [self setupAnnouncementCardWithEvent:event];
    
    // 更新滚动视图内容大小
    self.scrollView.contentSize = CGSizeMake(self.bounds.size.width, CGRectGetMaxY(self.announcementCard.frame) + 20);
}

- (void)setupHeaderCardWithEvent:(FishingEvent *)event {
    if (self.headerCard) {
        [self.headerCard removeFromSuperview];
    }
    
    CGFloat cardWidth = self.bounds.size.width - 32;
    self.headerCard = [[UIView alloc] initWithFrame:CGRectMake(16, 16, cardWidth, 80)];
    self.headerCard.backgroundColor = [UIColor systemBackgroundColor];
    self.headerCard.layer.cornerRadius = 12.0;
    self.headerCard.layer.masksToBounds = YES;
    [self.contentView addSubview:self.headerCard];
    
    // 已报名信息
    self.registrationLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 16, 120, 20)];
    self.registrationLabel.text = [NSString stringWithFormat:@"已报名 %ld/%ld", (long)event.registeredCount, (long)event.maxParticipants];
    self.registrationLabel.font = [UIFont systemFontOfSize:18 weight:UIFontWeightSemibold];
    self.registrationLabel.textColor = [UIColor labelColor];
    [self.headerCard addSubview:self.registrationLabel];
    
    // 查看全部按钮
    UIButton *viewAllBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    // 确保按钮在卡片内部
    CGFloat buttonX = cardWidth - 96;
    if (buttonX < 16) buttonX = 16; // 最小左边距
    viewAllBtn.frame = CGRectMake(buttonX, 12, 80, 28);
    [viewAllBtn setTitle:@"查看全部 >" forState:UIControlStateNormal];
    viewAllBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    viewAllBtn.layer.cornerRadius = 4;
    [viewAllBtn addTarget:self action:@selector(didTapViewAll) forControlEvents:UIControlEventTouchUpInside];
    [self.headerCard addSubview:viewAllBtn];
    
    // 添加调试日志
    NSLog(@"查看全部按钮已创建，cardWidth: %.1f, buttonX: %.1f, frame: %@", cardWidth, buttonX, NSStringFromCGRect(viewAllBtn.frame));
    
    // 头像组 - 第二行
    UIView *avatarContainer = [[UIView alloc] initWithFrame:CGRectMake(16, 48, 200, 24)];
    [self.headerCard addSubview:avatarContainer];
    
    NSArray *avatarColors = @[[UIColor systemBlueColor], [UIColor systemGreenColor], 
                             [UIColor systemOrangeColor], [UIColor systemPurpleColor]];
    for (int i = 0; i < MIN(event.participantAvatars.count, 4); i++) {
        UIView *avatar = [[UIView alloc] initWithFrame:CGRectMake(i * 28, 0, 24, 24)];
        avatar.backgroundColor = avatarColors[i];
        avatar.layer.cornerRadius = 12.0;
        avatar.layer.masksToBounds = YES;
        [avatarContainer addSubview:avatar];
    }
}

- (void)setupDetailsCardWithEvent:(FishingEvent *)event {
    if (self.detailsCard) {
        [self.detailsCard removeFromSuperview];
    }
    
    CGFloat cardWidth = self.bounds.size.width - 32;
    CGFloat y = 112; // 从头部卡片下方开始（80+16+16）
    
    self.detailsCard = [[UIView alloc] initWithFrame:CGRectMake(16, y, cardWidth, 0)];
    self.detailsCard.backgroundColor = [UIColor systemBackgroundColor];
    self.detailsCard.layer.cornerRadius = 12.0;
    self.detailsCard.layer.masksToBounds = YES;
    [self.contentView addSubview:self.detailsCard];
    
    // 活动信息标题
    UIView *indicator = [[UIView alloc] initWithFrame:CGRectMake(12, 18, 4, 18)];
    indicator.backgroundColor =whBlueColor;
    indicator.layer.cornerRadius = 2.0;
    [self.detailsCard addSubview:indicator];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(24, 12, 200, 28)];
    titleLabel.text = @"活动信息";
    titleLabel.font = [UIFont systemFontOfSize:20 weight:UIFontWeightBold];
    [self.detailsCard addSubview:titleLabel];
    
    // 活动详情列表
    NSArray *eventDetails = @[
        @[@"开钓时间", event.fishingStartTime],
        @[@"时长", [NSString stringWithFormat:@"%.1f小时", event.duration]],
        @[@"鱼塘", event.pondType],
        @[@"鱼种", event.fishSpecies],
        @[@"放鱼量", [NSString stringWithFormat:@"%.1f斤", event.fishQuantity]],
        @[@"支付方式", event.paymentMethod],
        @[@"支付类型", event.paymentType],
        @[@"是否回鱼", event.isFishReturn ? @"回鱼" : @"不回鱼"],
        @[@"报名开始时间", event.registrationStartTime],
        @[@"报名截止时间", event.registrationEndTime],
        @[@"抽号方式", event.lotteryMethod],
        @[@"抽号开始时间", event.lotteryStartTime],
        @[@"抽号结束时间", event.lotteryEndTime],
        @[@"限报人数", [NSString stringWithFormat:@"%ld人", (long)event.maxParticipants]],
        @[@"退款说明", event.refundPolicy],
        @[@"活动状态", event.eventStatus]
    ];
    
    CGFloat contentY = CGRectGetMaxY(titleLabel.frame) + 16;
    for (NSArray *detail in eventDetails) {
        UILabel *keyLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, contentY, 100, 20)];
        keyLabel.text = detail[0];
        keyLabel.font = [UIFont systemFontOfSize:14];
        keyLabel.textColor = [UIColor secondaryLabelColor];
        [self.detailsCard addSubview:keyLabel];
        
        UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, contentY, cardWidth - 136, 20)];
        valueLabel.text = detail[1];
        valueLabel.font = [UIFont systemFontOfSize:14];
        valueLabel.textColor = [UIColor labelColor];
        
        // 特殊颜色处理
        if ([detail[0] containsString:@"时间"] || [detail[0] containsString:@"截止"]) {
            valueLabel.textColor = [UIColor systemOrangeColor];
        } else if ([detail[0] containsString:@"人数"]) {
            valueLabel.textColor = whBlueColor;
        } else if ([detail[0] containsString:@"退款"]) {
            valueLabel.textColor = [UIColor systemRedColor];
        } else if ([detail[0] containsString:@"状态"]) {
            valueLabel.textColor = whBlueColor;
            valueLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightSemibold];
        }
        
        [self.detailsCard addSubview:valueLabel];
        contentY += 24;
    }
    
    // 更新卡片高度
    CGFloat detailsCardHeight = contentY + 16;
    self.detailsCard.frame = CGRectMake(16, y, cardWidth, detailsCardHeight);
}

- (void)setupAnnouncementCardWithEvent:(FishingEvent *)event {
    if (self.announcementCard) {
        [self.announcementCard removeFromSuperview];
    }
    
    CGFloat cardWidth = self.bounds.size.width - 32;
    // 动态计算公告卡片位置：头部卡片(112) + 活动信息卡片高度 + 间距
    CGFloat detailsCardBottom = 112 + (self.detailsCard ? self.detailsCard.frame.size.height : 424);
    CGFloat y = detailsCardBottom + 16; // 从活动信息卡片下方开始，增加间距
    
    self.announcementCard = [[UIView alloc] initWithFrame:CGRectMake(16, y, cardWidth, 0)];
    self.announcementCard.backgroundColor = [UIColor systemBackgroundColor];
    self.announcementCard.layer.cornerRadius = 12.0;
    self.announcementCard.layer.masksToBounds = YES;
    [self.contentView addSubview:self.announcementCard];
    
    // 公告说明标题
    UIView *indicator = [[UIView alloc] initWithFrame:CGRectMake(12, 18, 4, 18)];
    indicator.backgroundColor = whBlueColor;
    indicator.layer.cornerRadius = 2.0;
    [self.announcementCard addSubview:indicator];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(24, 12, 200, 28)];
    titleLabel.text = @"公告说明";
    titleLabel.font = [UIFont systemFontOfSize:20 weight:UIFontWeightBold];
    [self.announcementCard addSubview:titleLabel];
    
    // 倒计时
    self.countdownLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(titleLabel.frame) + 8, cardWidth - 32, 24)];
    self.countdownLabel.text = @"距离报名结束时间还剩: 01天 13:20:20";
    self.countdownLabel.font = [UIFont systemFontOfSize:16];
    self.countdownLabel.textColor = whBlueColor;
    [self.announcementCard addSubview:self.countdownLabel];
    
    // 公告内容
    self.announcementTextView = [[UITextView alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(self.countdownLabel.frame) + 8, cardWidth - 32, 0)];
    self.announcementTextView.text = event.announcement;
    self.announcementTextView.font = [UIFont systemFontOfSize:14];
    self.announcementTextView.textColor = [UIColor labelColor];
    self.announcementTextView.backgroundColor = [UIColor clearColor];
    self.announcementTextView.editable = NO;
    self.announcementTextView.scrollEnabled = NO;
    [self.announcementCard addSubview:self.announcementTextView];
    
    // 计算内容高度
    CGSize contentSize = [event.announcement boundingRectWithSize:CGSizeMake(cardWidth - 32, CGFLOAT_MAX)
                                                          options:NSStringDrawingUsesLineFragmentOrigin
                                                       attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]}
                                                          context:nil].size;
    
    self.announcementTextView.frame = CGRectMake(16, CGRectGetMaxY(self.countdownLabel.frame) + 8, cardWidth - 32, ceil(contentSize.height) + 20);
    
    // 更新卡片高度
    self.announcementCard.frame = CGRectMake(16, y, cardWidth, CGRectGetMaxY(self.announcementTextView.frame) + 16);
}

- (void)updateCountdownText:(NSString *)countdownText {
    self.countdownLabel.text = countdownText;
}

- (void)updateRegistrationCount:(NSInteger)count {
    self.registrationLabel.text = [NSString stringWithFormat:@"已报名 %ld/%ld", (long)count, (long)38];
}

#pragma mark - Actions

- (void)didTapViewAll {
    NSLog(@"查看全部按钮被点击");
    if ([self.delegate respondsToSelector:@selector(fishingEventViewDidTapViewAll)]) {
        NSLog(@"代理方法被调用");
        [self.delegate fishingEventViewDidTapViewAll];
    } else {
        NSLog(@"代理未设置或未实现方法");
    }
}
    
@end
